public class Solution35 {
    public int searchInsert(int[] nums, int target) {
        /*
         int index = Arrays.binarySearch(nums, target);
         if (index < 0)
             index = -(index + 1);
         return index;
        */
        int left = 0, right = nums.length, mid;
        while (left <= right) {
            mid = (left + right) / 2;
            if (mid >= nums.length || nums[mid] == target)
                return mid;
            else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return left;
    }
}
